package com.education.platform.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import com.education.platform.common.Result;
import com.education.platform.dto.auth.*;
import com.education.platform.service.AuthService;
import com.education.platform.service.SmsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;

@Tag(name = "用户认证", description = "用户注册、登录、密码管理等接口")
@RestController
@RequestMapping("/api/auth")
@RequiredArgsConstructor
public class AuthController {

    private final AuthService authService;
    private final SmsService smsService;

    @Operation(summary = "用户注册")
    @PostMapping("/register")
    public Result<AuthTokenVO> register(@Validated @RequestBody RegisterDTO registerDTO, 
                                       HttpServletRequest request) {
        AuthTokenVO tokenVO = authService.register(registerDTO, getClientInfo(request));
        return Result.success(tokenVO);
    }

    @Operation(summary = "用户登录")
    @PostMapping("/login")
    public Result<AuthTokenVO> login(@Validated @RequestBody LoginDTO loginDTO, 
                                    HttpServletRequest request) {
        AuthTokenVO tokenVO = authService.login(loginDTO, getClientInfo(request));
        return Result.success(tokenVO);
    }

    @Operation(summary = "发送短信验证码")
    @PostMapping("/sms-send")
    public Result<Void> sendSmsCode(@Validated @RequestBody SmsCodeDTO smsCodeDTO) {
        smsService.sendCode(smsCodeDTO.getMobile(), smsCodeDTO.getType());
        return Result.success();
    }

    @Operation(summary = "退出登录")
    @PostMapping("/logout")
    @SaCheckLogin
    public Result<Void> logout() {
        StpUtil.logout();
        return Result.success();
    }

    @Operation(summary = "刷新Token")
    @PostMapping("/refresh")
    @SaCheckLogin
    public Result<AuthTokenVO> refresh() {
        AuthTokenVO tokenVO = authService.refreshToken();
        return Result.success(tokenVO);
    }

    @Operation(summary = "修改密码")
    @PostMapping("/change-password")
    @SaCheckLogin
    public Result<Void> changePassword(@Validated @RequestBody ChangePasswordDTO changePasswordDTO) {
        authService.changePassword(changePasswordDTO);
        return Result.success();
    }

    @Operation(summary = "忘记密码")
    @PostMapping("/forgot-password")
    public Result<Void> forgotPassword(@Validated @RequestBody ForgotPasswordDTO forgotPasswordDTO) {
        authService.forgotPassword(forgotPasswordDTO);
        return Result.success();
    }

    private String getClientInfo(HttpServletRequest request) {
        String ip = request.getRemoteAddr();
        String userAgent = request.getHeader("User-Agent");
        return ip + "|" + userAgent;
    }
} 